platform :ios, '15.0'
use_modular_headers!

# CocoaPods analytics sends network stats synchronously affecting flutter build latency.
ENV['COCOAPODS_DISABLE_STATS'] = 'true'

project 'Runner', {
  'Debug' => :debug,
  'Profile' => :release,
  'Release' => :release,
}

def flutter_root
  generated_xcode_build_settings_path = File.expand_path(File.join('..', 'Flutter', 'Generated.xcconfig'), __FILE__)
  unless File.exist?(generated_xcode_build_settings_path)
    raise "#{generated_xcode_build_settings_path} must exist. If you're running pod install manually, make sure flutter pub get is executed first"
  end

  File.foreach(generated_xcode_build_settings_path) do |line|
    matches = line.match(/FLUTTER_ROOT\=(.*)/)
    return matches[1].strip if matches
  end
  raise "FLUTTER_ROOT not found in #{generated_xcode_build_settings_path}. Try deleting Generated.xcconfig, then run flutter pub get"
end

require File.expand_path(File.join('packages', 'flutter_tools', 'bin', 'podhelper'), flutter_root)

flutter_ios_podfile_setup

target 'Runner' do
  use_frameworks!
  use_modular_headers!

  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
  
  target 'RunnerTests' do
    inherit! :search_paths
  end
end

post_install do |installer|
  installer.pods_project.targets.each do |target|
    # Set deployment target for all configs first to ensure consistency
    target.build_configurations.each do |config|
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '15.0'
    end
    
    # Apply Flutter specific settings (called per target, not per config)
    flutter_additional_ios_build_settings(target)
    
    # Then apply per-config settings
    target.build_configurations.each do |config|
      # Ensure deployment target is still 15.0 after Flutter settings
      config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '15.0'
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
        '$(inherited)',
        # dart: PermissionGroup.notification
        'PERMISSION_NOTIFICATIONS=1',
      ]
      
      # Add app tracking transparency framework
      config.build_settings['OTHER_LDFLAGS'] ||= '$(inherited)'
      config.build_settings['OTHER_LDFLAGS'] += ' -framework AppTrackingTransparency'

      # Exclude arm64 for simulator builds (Apple Silicon workaround)
      # This is needed because some pods (like SuperwallKit, Stripe) don't support arm64 simulator
      config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'arm64'
      
      # Specifically handle pods that don't support arm64 simulator
      if target.name.include?('SuperwallKit') || target.name.include?('Stripe')
        config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'arm64'
        config.build_settings['ONLY_ACTIVE_ARCH'] = 'NO'
      end
      
      # Exclude TikTok SDK from simulator builds (doesn't support simulator architectures)
      if target.name.include?('TikTok') || target.name.include?('tiktok')
        config.build_settings['EXCLUDED_ARCHS[sdk=iphonesimulator*]'] = 'arm64 x86_64'
        config.build_settings['ONLY_ACTIVE_ARCH'] = 'NO'
      end

      # Finally, force Automatic signing for Pods targets AFTER other settings
      config.build_settings['CODE_SIGN_STYLE'] = 'Automatic'
      config.build_settings['DEVELOPMENT_TEAM'] = nil
      config.build_settings['CODE_SIGN_IDENTITY'] = ''
      config.build_settings['PROVISIONING_PROFILE_SPECIFIER'] = ''
      config.build_settings['PROVISIONING_PROFILE'] = ''
    end 
  end
  
  # Run script to remove TikTok SDK frameworks from Debug builds (simulator only)
  fix_script = File.join(File.dirname(__FILE__), 'fix_tiktok_simulator.rb')
  if File.exist?(fix_script)
    system("ruby #{fix_script}")
  end
end
